package edu.towson.cis.cosc490.cero.midterm.main;

import java.util.ArrayList;
import java.util.List;

import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import edu.towson.cis.cosc490.cero.midterm.data.Constants;
import edu.towson.cis.cosc490.cero.midterm.data.ArticleData;
import edu.towson.cis.cosc490.cero.midterm.parsers.AndroidSaxFeedParser;
import edu.towson.cis.cosc490.cero.orioles.R;

//main activity for Orioles RSS reader
public class OriolesMainActivity extends ListActivity {

	private List<NewsItem> articles;
	private ArticleData articleData;
	private boolean serviceFlag;
	AlarmManager alarms;
	private String feedURL = "http://partner.mlb.com/partnerxml/gen/news/rss/bal.xml";

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);

		setContentView(R.layout.main);
		articleData = new ArticleData(this);
		loadFeed();
		setAlarm();
	}

	public void onDestroy(){
		super.onDestroy();
		stopService(new Intent(OriolesMainActivity.this,MusicService.class));
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent viewMessage = new Intent(Intent.ACTION_VIEW, Uri.parse(articles
				.get(position).getLink().toExternalForm()));
		startActivity(viewMessage);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case R.id.Refresh:
			loadFeed();
			break;
		case R.id.Start:
			if (!serviceFlag)
				setAlarm();
			break;
		case R.id.Stop:
			if (serviceFlag)
				cancelAlarm();
			break;

		case R.id.Standings:
			getStandings();
			break;
		}
		return true;
	}

	// opens up MLB official standings page
	private void getStandings() {
		String url = "http://m.mlb.com/standings/";
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}

	private void setAlarm() {
		serviceFlag = true;
		Intent downloader = new Intent(this, AlarmReceiver.class);
		PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0,
				downloader, PendingIntent.FLAG_CANCEL_CURRENT);

		alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
		alarms.setRepeating(AlarmManager.RTC_WAKEUP,
				SystemClock.elapsedRealtime(), 60000, recurringDownload);
		System.out.println("Setting alarm...");
	}

	private void cancelAlarm() {
		serviceFlag = false;
		Intent downloader = new Intent(this, AlarmReceiver.class);
		PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0,
				downloader, PendingIntent.FLAG_CANCEL_CURRENT);

		alarms.cancel(recurringDownload);
	}

	private void loadFeed() {
		try {

			AndroidSaxFeedParser parser = new AndroidSaxFeedParser(feedURL);
			articles = parser.parse();

			List<String> titles = new ArrayList<String>(articles.size());

			for (NewsItem rd : articles) {
				titles.add(rd.getTitle());

				try {
					addNewsItem(rd.getDescription(), "" + rd.getTitle(), rd
							.getLink().toString(), rd.getDate());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					R.layout.news_item, titles);
			this.setListAdapter(adapter);
		} catch (Throwable t) {
		}
	}

	private void addNewsItem(String des, String title, String link,
			String pubDate) {
		SQLiteDatabase db = articleData.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(Constants.DES, des);
		values.put(Constants.TITLE, title);
		values.put(Constants.LINK, link);
		values.put(Constants.PUBDATE, pubDate);
		db.insertOrThrow(Constants.TABLE_NAME, null, values);
	}
}
